/*
 *    Document   : StVideoAula
 *    Created on : 05/03/2013, 11:58:59
 *    Author     : Fiúza <rbfiuza@gmail.com>
 */



Ext.define('FC.controller.StVideoAula', {
    extend: 'Ext.app.Controller',

    stores:['StVideoAula'],
    models:['StVideoAula'],
    views:['StVideoAulaList'],
   
    init : function() {
        this.control({
                    
   
            'stVideoAulaList': {
                edit: this.editStVideoAula
                
            }
        });
    },
    
    editStVideoAula : function(editor, obj) {
        //check if record is dirty
        if(obj.record.dirty){
            //check if the record is valid   
//            console.log(obj.record.validate());
            if(obj.record.validate().isValid()){
                //Make your Ajax request to sync data
                mode = (obj.record.get('userId') === "") ? 'insert': 'update';
                this.syncData(obj, mode);
            }
        }
    },
            
    //Sync data with the server 
    syncData : function(obj, mode) {
        Ext.Ajax.request({
            url: 'atualizaStVideoAula',
            params: {
                store_id: 1,
                action: mode,
                rowIndex: obj.rowIdx,
                recordInfo: Ext.encode(this.getStVideoAulaStore().getAt(obj.rowIdx).data)
            },
            scope:this,
            //method to call when the request is successful
            success: this.onSaveSuccess,
            //method to call when the request is a failure
            failure: this.onSaveFailure
        });
    },

    onSaveFailure: function(err){
        //Alert the user about communication error
        Ext.MessageBox.alert('Status', 'Error occured during update stVideoAula');
    },
    
    onSaveSuccess: function(response,opts){
        //Remove dirty
        var ajaxResponse = Ext.decode(response.responseText);
        if(ajaxResponse.success){
            //if we are doing an insert then get the new customerId 
            //and update the store record
            if(opts.params.action === 'insert'){
                stVideoAulaId = ajaxResponse.stVideoAulaId;
                this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('stVideoAula',stVideoAulaId);
            }

//            this.getDiariasStore().getAt(opts.params.rowIndex).set('id',ajaxResponse.diarias.polos);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('dscStVideoAula',ajaxResponse.videoaula.dscStVideoAula);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('qntDisciplinas',ajaxResponse.videoaula.qntDisciplinas);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('qntVideoAula',ajaxResponse.videoaula.qntVideoAula);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('qntTotal',ajaxResponse.videoaula.qntTotal);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('vlrUnit',ajaxResponse.videoaula.vlrUnit);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('vlrFinal',ajaxResponse.videoaula.vlrFinal);            
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).set('indicacoesParametros',ajaxResponse.videoaula.indicacoesParametros);
            this.getStVideoAulaStore().getAt(opts.params.rowIndex).commit();
//            this.getDiariasStore().save();

        }
        else {
            Ext.MessageBox.alert('Status', 'Error occured during update stVideoAula');
        }


//      e.record.set(e.field,gridValue);
//      e.record.commit();
//      myGrid.getView().refresh();
    

    
    }
});